package simpleSort;

public class SortingHelper {


    public static <E extends  Comparable<E>> boolean isSorted(E[] arr){
        for (int i = 1; i < arr.length; i++) {
            if (arr[i-1].compareTo(arr[i]) > 0){
//                System.out.println(arr[i-1]);
//                System.out.println(arr[i]);
//                System.out.println(i);
//                for (int j = 0; j < arr.length ; j++) {
//                    System.out.print(arr[j]+" ");
//                }
                return false;
            }
        }
        return true;
    }

    public static <E extends Comparable<E>> void sortTest(String sortName,E[] arr){
        long start = System.nanoTime();
        if ("selectSort".equals(sortName)){
//            SelectSort.sort(arr);
            SelectSort.descSort(arr);
        }else if("insertSort".equals(sortName)){
            InsertSort.sort(arr);
        }else if("insertSort2".equals(sortName)){
            InsertSort.sort2(arr);
        }else if("insertDescSort".equals(sortName)){
            InsertSort.descSort(arr);
        }
        long end = System.nanoTime();
        double time = (end-start)/1000000000.0;
        if (!SortingHelper.isSorted(arr)){
            throw new RuntimeException(sortName + " is failed");
        }
        String format = String.format("%s, n=%d, time=%f s", sortName, arr.length, time);
        System.out.println(format);
    }

}
